package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Activity;
import com.qk.mapper.ActivityMapper;
import com.qk.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 活动管理Service实现类
 */
@Service
public class ActivityServiceImpl implements ActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 新增活动
     */
    @Override
    public void addActivity(Activity activity) {
        //1. 设置创建时间和更新时间
        activity.setCreateTime(LocalDateTime.now());
        activity.setUpdateTime(LocalDateTime.now());
        //2. 调用Mapper层插入数据
        activityMapper.insert(activity);
    }

    /**
     * 查询活动列表
     */
    @Override
    public PageResult<Activity> list(Integer channel, Integer type, Integer status, int page, int pageSize) {
        // 开始分页
        PageHelper.startPage(page, pageSize);

        // 执行查询
        List<Activity> activities = activityMapper.list(channel, type, status);

        // 获取分页信息
        Page<Activity> pageInfo = (Page<Activity>) activities;

        // 封装成 PageResult 对象
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getResult());
    }

    @Override
    public Activity getActivityById(Integer id) {
        return activityMapper.getActivityById(id);
    }

    @Override
    public void updateActivity(Activity activity) {
        //1. 设置更新时间
        activity.setUpdateTime(LocalDateTime.now());
        //2. 调用Mapper层更新数据
        activityMapper.updateActivity(activity);
    }

    @Override
    public void deleteActivity(Integer id) {
        activityMapper.deleteActivity(id);
    }

    @Override
    public List<Activity> getActivitiesByType(Integer type) {
        return activityMapper.selectActivitiesByType(type);
    }
}